package com.ayf.payment.game.api.controller.platform;

import com.ayf.payment.game.api.dto.platform.AdminRoleDTO;
import com.ayf.payment.game.api.dto.platform.AdminRoleMenuDTO;
import com.ayf.payment.game.api.dto.platform.AdminUserDTO;
import com.ayf.payment.game.api.dto.platform.TokenAdmin;
import com.ayf.payment.game.api.dto.platform.criteria.AdminLogPageCriteria;
import com.ayf.payment.game.api.dto.platform.criteria.AdminUserCriteria;
import com.ayf.payment.game.api.dto.platform.criteria.AdminUserPageCriteria;
import com.ayf.payment.game.api.entity.platform.AdminLog;
import com.ayf.payment.game.api.entity.platform.AdminRole;
import com.ayf.payment.game.api.entity.platform.AdminUser;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.platform.*;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.IPUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author wwl
 * @date 2020/3/4 10:41
 */
@RequestMapping("/t/admin")
@RestController
public class AdminManagerController {

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    AdminRoleService adminRoleService;

    @Autowired
    private TokenAdminService tokenAdminService;

    @Autowired
    private AdminPermissionService adminPermissionService;

    @Autowired
    private AdminLogService adminLogService;

    /**
     * 登录
     *
     * @param adminUserCriteria
     * @return
     */
    @PostMapping("/login")
    public CommonResult<Map<String, Object>> login(@RequestBody AdminUserCriteria adminUserCriteria, HttpServletRequest request) {
        String ip = IPUtils.getIp(request);
        adminUserCriteria.setLoginIp(ip);
        if(adminUserCriteria==null){
            throw new ParameterException("账号或密码错误");
        }
        if(StringUtils.isEmpty(adminUserCriteria.getLoginAccount())){
            throw new ParameterException("账号或密码错误");
        }
        if(StringUtils.isEmpty(adminUserCriteria.getPassword())){
            throw new ParameterException("账号或密码错误");
        }
        AdminUser adminUser = adminUserService.login(adminUserCriteria);
        tokenAdminService.deleteToken(adminUser.getId(),ip);
        TokenAdmin token = tokenAdminService.createToken(adminUser.getId(),ip);
        Map<String, Object> result = new HashMap<>(2);
        AdminRole adminRole=new AdminRole();
        adminRole.setId(adminUser.getRoleId());
        List<AdminRoleMenuDTO> roleMenuList=adminPermissionService.selectRoleMenuTree(adminRole);
        result.put("accessTokenAdmin", token.getAccessToken());
        result.put("roleMenuList",roleMenuList);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "登录成功", result);
    }

    /**
     * 退出
     *
     * @param adminUserDTO
     * @return
     */
    @PostMapping("/logout")
    public CommonResult logout(AdminUserDTO adminUserDTO, HttpServletRequest request) {
        String ip = IPUtils.getIp(request);
        tokenAdminService.deleteToken(adminUserDTO.getId(),ip);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,ip,"退出登录","退出登录",new Date());
        adminLogService.insert(adminLog);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "退出成功", "");
    }

    /**
     *  日志列表查询
     * @return
     */
    @PostMapping("/selectAdminLogPage")
    public CommonResult<PageInfo<AdminLog>> selectAdminLog(AdminUserDTO adminUserDTO,@RequestBody AdminLogPageCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        if(adminUserDTO.getRoleId()!=12){
            criteria.setName(adminUserDTO.getLoginAccount());
        }
        List<AdminLog> list= adminLogService.selectPage(criteria);
        PageInfo<AdminLog> page= new PageInfo<AdminLog>(list);
        return new CommonResult<PageInfo<AdminLog>>(CommonConstants.RETURN_SUCCESS, "成功", page);
    }

    /**
     *  删除日志
     * @return
     */
    @PostMapping("/delAdminLog")
    public CommonResult delAdminLog(@RequestBody AdminUserCriteria criteria) {
        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        AdminLog log=new AdminLog();
        log.setId(criteria.getId());
        adminLogService.delAdminLog(log);
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "删除成功", "");
    }

    /**
     *  用户列表
     * @return
     */
    @PostMapping("/selectUserPage")
    public CommonResult<PageInfo<AdminUserDTO>> selectAdminUser(@RequestBody AdminUserPageCriteria criteria) {
        PageInfo<AdminUserDTO> page= adminUserService.selectAllPage(criteria);
        return new CommonResult<PageInfo<AdminUserDTO>>(CommonConstants.RETURN_SUCCESS, "成功", page);
    }

    /**
     *  获取当前用户信息
     * @return
     */
    @PostMapping("/getUser")
    public CommonResult<AdminUserDTO> getUser(AdminUserDTO adminUserDTO, HttpServletRequest request) {
        //adminUserDTO.setLoginIp(IPUtils.getIp(request));
        return new CommonResult<AdminUserDTO>(CommonConstants.RETURN_SUCCESS, "成功", adminUserDTO);
    }

    /**
     *  获取修改用户信息
     * @return
     */
    @PostMapping("/getSubUser")
    public CommonResult<AdminUserDTO> getSubUser(AdminUserDTO adminUserDTO, @RequestBody AdminUserCriteria criteria) {
        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        if(criteria.getId()==null){
            throw new ParameterException("账号不能为空");
        }
        AdminUser user=new AdminUser();
        user.setId(criteria.getId());
        AdminUserDTO userDb= adminUserService.selectOneDTO(user);
        return new CommonResult<AdminUserDTO>(CommonConstants.RETURN_SUCCESS, "修改成功", userDb);
    }

    /**
     *  删除修改用户
     * @return
     */
    @PostMapping("/delUser")
    public CommonResult delUser(AdminUserDTO adminUserDTO, @RequestBody AdminUserCriteria criteria, HttpServletRequest request) {
        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        if(criteria.getId()==null){
            throw new ParameterException("账号不能为空");
        }
        if(criteria.getId()==1){
            throw new ParameterException("超级管理员不能删除");
        }
        String ip = IPUtils.getIp(request);
        AdminUser user=new AdminUser();
        user.setId(criteria.getId());
        AdminUserDTO userDto= adminUserService.selectOneDTO(user);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,ip,"删除用户："+userDto.getLoginAccount(),"删除用户",new Date());
        adminUserService.del(user);
        adminLogService.insert(adminLog);
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "删除成功", "");
    }

    /**
     *  用户创建
     * @return
     */
    @PostMapping("/createAdminUser")
    public CommonResult createAdminUser(AdminUserDTO adminUserDTO, @RequestBody AdminUserCriteria criteria, HttpServletRequest request) {

        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        if(StringUtils.isEmpty(criteria.getLoginAccount())){
            throw new ParameterException("账号不能为空");
        }

        if(StringUtils.isEmpty(criteria.getPassword())){
            throw new ParameterException("密码不能为空");
        }
        if(StringUtils.isEmpty(criteria.getRePassword())){
            throw new ParameterException("重复密码不能为空");
        }
        if(!criteria.getPassword().equals(criteria.getRePassword())){
            throw new ParameterException("密码不一致");
        }
        if(criteria.getRoleId()==null){
            throw new ParameterException("角色不能为空");
        }
        if(StringUtils.isEmpty(criteria.getAccountNumber())){
            throw new ParameterException("员工工号不能为空");
        }
        if(StringUtils.isEmpty(criteria.getRealName())){
            throw new ParameterException("姓名不能为空");
        }
        if(StringUtils.isEmpty(criteria.getPhone())){
            throw new ParameterException("电话不能为空");
        }
        String ip = IPUtils.getIp(request);
        criteria.setLoginIp(ip);
        adminUserService.insert(criteria,adminUserDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "创建成功", "");
    }

    /**
     *  用户信息修改
     * @return
     */
    @PostMapping("/updateAdminUser")
    public CommonResult updateAdminUser(AdminUserDTO adminUserDTO, @RequestBody AdminUserCriteria criteria, HttpServletRequest request) {
        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        if(StringUtils.isEmpty(criteria.getLoginAccount())){
            throw new ParameterException("账号不能为空");
        }
        if(criteria.getRoleId()==null){
            throw new ParameterException("角色不能为空");
        }
        if(StringUtils.isEmpty(criteria.getRealName())){
            throw new ParameterException("姓名不能为空");
        }
        if(StringUtils.isEmpty(criteria.getPhone())){
            throw new ParameterException("电话不能为空");
        }
        if(StringUtils.isEmpty(criteria.getAccountNumber())){
            throw new ParameterException("员工工号不能为空");
        }
        String ip = IPUtils.getIp(request);
        criteria.setLoginIp(ip);
        adminUserService.update(criteria,adminUserDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功", "");
    }

    /**
     *  用户修改密码
     * @return
     */
    @PostMapping("/updateUserPwd")
    public CommonResult updateAdminUserPwd(AdminUserDTO adminUserDTO, @RequestBody AdminUserCriteria criteria, HttpServletRequest request) {
        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        criteria.setId(adminUserDTO.getId());
        if(StringUtils.isEmpty(criteria.getPassword())){
            throw new ParameterException("密码不能为空");
        }
        if(StringUtils.isEmpty(criteria.getRePassword())){
            throw new ParameterException("重复密码不能为空");
        }
        if(!criteria.getPassword().equals(criteria.getRePassword())){
            throw new ParameterException("密码不一致");
        }
        String ip = IPUtils.getIp(request);
        criteria.setLoginIp(ip);
        adminUserService.updatePWD(criteria);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),criteria.getId(),0,criteria.getLoginIp(),"更新自己的密码","更新密码",new Date());
        adminLogService.insert(adminLog);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功", "");
    }

    /**
     *  管理员给用户修改密码
     * @return
     */
    @PostMapping("/updateAdminPwd")
    public CommonResult updatePwd(AdminUserDTO adminUserDTO, @RequestBody AdminUserCriteria criteria, HttpServletRequest request) {
        if(criteria==null){
            throw new ParameterException("参数不能为空");
        }
        if(criteria.getId()==null){
            throw new ParameterException("账号不能为空");
        }
        if(StringUtils.isEmpty(criteria.getPassword())){
            throw new ParameterException("密码不能为空");
        }
        if(StringUtils.isEmpty(criteria.getRePassword())){
            throw new ParameterException("重复密码不能为空");
        }
        if(!criteria.getPassword().equals(criteria.getRePassword())){
            throw new ParameterException("密码不一致");
        }
        String ip = IPUtils.getIp(request);
        criteria.setLoginIp(ip);
        adminUserService.updatePWD(criteria);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,criteria.getLoginIp(),"给用户"+criteria.getId()+"更新密码","更新密码",new Date());
        adminLogService.insert(adminLog);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功", "");
    }

    /**
     *  角色列表
     * @return
     */
    @PostMapping("/selectRoleAll")
    public CommonResult<List<AdminRole>> selectRoleAll() {
        List<AdminRole> list= adminRoleService.selectAll();
        return new CommonResult<List<AdminRole>>(CommonConstants.RETURN_SUCCESS, "查询成功", list);
    }

    /**
     *  获取所有权限列表
     * @return
     */
    @PostMapping("/selectMenuAll")
    public CommonResult<List<AdminRoleMenuDTO>> selectPermissionAll() {
        List<AdminRoleMenuDTO> list= adminPermissionService.selectMenuTree();
        return new CommonResult<List<AdminRoleMenuDTO>>(CommonConstants.RETURN_SUCCESS, "查询成功", list);
    }

    /**
     *  获取所有权限列表
     * @return
     */
    @PostMapping("/selectRoleMenuAll")
    public CommonResult<List<AdminRoleMenuDTO>> selectRoleMenuAll(@RequestBody AdminRoleDTO adminRoleDTO) {
        AdminRole adminRole=new AdminRole();
        adminRole.setId(adminRoleDTO.getId());
        List<AdminRoleMenuDTO> list= adminPermissionService.selectRoleMenuTree(adminRole);
        return new CommonResult<List<AdminRoleMenuDTO>>(CommonConstants.RETURN_SUCCESS, "查询成功", list);
    }


    /**
     * 创建角色
     *
     * @param adminRoleDTO
     * @return
     */
    @PostMapping("/saveRole")
    public CommonResult saveRoleAll(AdminUserDTO adminUserDTO, @RequestBody AdminRoleDTO adminRoleDTO, HttpServletRequest request) {
      if(adminRoleDTO==null){
          throw new ParameterException("参数不能为空");
      }
        if(StringUtils.isEmpty(adminRoleDTO.getName())){
            throw new ParameterException("角色名不能为空");
        }
        String ip = IPUtils.getIp(request);

        adminRoleService.insertRoleAndPermission(adminRoleDTO);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,ip,"创建角色："+adminRoleDTO.getName(),"创建角色",new Date());
        adminLogService.insert(adminLog);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "创建成功", "");
    }

    /**
     * 角色按ID查询
     *
     * @param adminRoleDTO
     * @return
     */
    @PostMapping("/selectRole")
    public CommonResult<AdminRoleDTO> selectRole(@RequestBody AdminRoleDTO adminRoleDTO) {
        if(adminRoleDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(adminRoleDTO.getId()==null){
            throw new ParameterException("角色不能为空");
        }
        AdminRole role=new AdminRole();
        role.setId(adminRoleDTO.getId());
        AdminRoleDTO roleData= adminRoleService.selectAdminRoleAndPermission(role);
        return new CommonResult<AdminRoleDTO>(CommonConstants.RETURN_SUCCESS, "查询成功", roleData);
    }


    /**
     * 角色修改
     *
     * @param adminRoleDTO
     * @return
     */
    @PostMapping("/updateRole")
    public CommonResult updateRole(AdminUserDTO adminUserDTO, @RequestBody AdminRoleDTO adminRoleDTO, HttpServletRequest request) {
        if(adminRoleDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(adminRoleDTO.getId()==null){
            throw new ParameterException("角色不能为空");
        }
        if(StringUtils.isEmpty(adminRoleDTO.getName())){
            throw new ParameterException("角色名不能为空");
        }
        String ip = IPUtils.getIp(request);
        AdminRole role=new AdminRole();
        role.setId(adminRoleDTO.getId());
        role.setName(adminRoleDTO.getName());
        role.setRemark(adminRoleDTO.getRemark());
        adminRoleService.update(role);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,ip,"角色修改："+adminRoleDTO.getName(),"修改角色",new Date());
        adminLogService.insert(adminLog);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功", "");
    }

    /**
     * 权限修改
     *
     * @param adminRoleDTO
     * @return
     */
    @PostMapping("/updateRoleAndPermission")
    public CommonResult updateRoleAndPermission(@RequestBody AdminRoleDTO adminRoleDTO) {
        if(adminRoleDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(adminRoleDTO.getId()==null){
            throw new ParameterException("角色不能为空");
        }
        if(adminRoleDTO.getId()==12){
            throw new ParameterException("超级管理员角色不能修改");
        }
        if(adminRoleDTO.getPermissionIds()==null && adminRoleDTO.getPermissionIds().length<=0 ){
            throw new ParameterException("角色名权限不能为空");
        }
        adminRoleService.updateRoleAndPermission(adminRoleDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功", "");
    }

    /**
     * 角色删除
     *
     * @param adminRoleDTO
     * @return
     */
    @PostMapping("/delRole")
    public CommonResult delRole(AdminUserDTO adminUserDTO, @RequestBody AdminRoleDTO adminRoleDTO, HttpServletRequest request) {
        if(adminRoleDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(adminRoleDTO.getId()==null){
            throw new ParameterException("请选角色");
        }
        if(adminRoleDTO.getId()==12){
            throw new ParameterException("超级管理员角色不能删除");
        }
        adminUserDTO=new AdminUserDTO();
        adminUserDTO.setLoginAccount("ad");
        adminUserDTO.setId(1);
        String ip = IPUtils.getIp(request);
        AdminRole role=new AdminRole();
        role.setId(adminRoleDTO.getId());
        role=adminRoleService.selectOne(role);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,ip,"角色删除："+role.getName(),"删除角色",new Date());
        adminRoleService.del(role);
        adminLogService.insert(adminLog);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "删除成功", "");
    }


}
